﻿// <copyright file="DomainTurnImporter.cs" company="Gravity Age Studios">
// Code released under the MIT license (see License.txt).
// </copyright>
// <author>Vicente Cartas Espinel</author>
// <email>vicente.cartas@gmail.com</email>
namespace LightSwitchApplication.Import
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    internal class DomainTurnImporter : VisualCollectionImporter<DomainTurn>
    {
        private readonly int Visibility = 0;
        private readonly int State = 1;
        private readonly int Domain = 2;
        private readonly int TurnNumber = 3;
        private readonly int OwnerInformation = 4;
        private readonly int GMInformation = 5;
        private readonly int FirstRoundOwnerInformation = 6;
        private readonly int SecondRoundOwnerInformation = 7;
        private readonly int ThirdRoundOwnerInformation = 8;
        private readonly int FirstRoundGMInformation = 9;
        private readonly int SecondRoundGMInformation = 10;
        private readonly int ThirdRoundGMInformation = 11;

        private IEnumerable<Domain> domains;

        public DomainTurnImporter(DataWorkspace workspace)
            : base(workspace)
        {
        }

        protected override void FetchRequisites()
        {
                        this.domains = this.workspace.ApplicationData.Domains.GetQuery().Execute();
        }

        protected override bool SkipEntity(int index)
        {
            if (this.headers[Domain].HasValue && this.headers[TurnNumber].HasValue)
            {
                if (this.entities.Any(e => e.Domain.Name.Equals(data[index + headers[Domain].Value].Trim(), StringComparison.InvariantCultureIgnoreCase) &&
                                           e.Turn.ToString().Equals(data[index + headers[TurnNumber].Value].Trim(), StringComparison.InvariantCultureIgnoreCase)))
                {
                    return true;
                }
            }

            return false;
        }

        protected override int?[] MapHeaders(string[] stringHeaders)
        {
            int?[] headers = new int?[12];

            for (int i = 0; i < stringHeaders.Length; i++)
            {
                if (stringHeaders[i].StartsWith("Vis", StringComparison.InvariantCultureIgnoreCase))
                {
                    headers[Visibility] = i;
                }
                else if (stringHeaders[i].StartsWith("Sta", StringComparison.InvariantCultureIgnoreCase))
                {
                    headers[State] = i;
                }
                else if (stringHeaders[i].StartsWith("Dom", StringComparison.InvariantCultureIgnoreCase))
                {
                    headers[Domain] = i;
                }
                else if (stringHeaders[i].StartsWith("TurnNum", StringComparison.InvariantCultureIgnoreCase))
                {
                    headers[TurnNumber] = i;
                }
                else if (stringHeaders[i].StartsWith("Own", StringComparison.InvariantCultureIgnoreCase))
                {
                    headers[OwnerInformation] = i;
                }
                else if (stringHeaders[i].StartsWith("GM", StringComparison.InvariantCultureIgnoreCase))
                {
                    headers[GMInformation] = i;
                }
                else if (stringHeaders[i].StartsWith("FirstRoundOwn", StringComparison.InvariantCultureIgnoreCase))
                {
                    headers[FirstRoundOwnerInformation] = i;
                }
                else if (stringHeaders[i].StartsWith("SecondRoundOwn", StringComparison.InvariantCultureIgnoreCase))
                {
                    headers[SecondRoundOwnerInformation] = i;
                }
                else if (stringHeaders[i].StartsWith("ThirdRoundOwn", StringComparison.InvariantCultureIgnoreCase))
                {
                    headers[ThirdRoundOwnerInformation] = i;
                }
                else if (stringHeaders[i].StartsWith("FirstRoundGM", StringComparison.InvariantCultureIgnoreCase))
                {
                    headers[FirstRoundGMInformation] = i;
                }
                else if (stringHeaders[i].StartsWith("SecondRoundGM", StringComparison.InvariantCultureIgnoreCase))
                {
                    headers[SecondRoundGMInformation] = i;
                }
                else if (stringHeaders[i].StartsWith("ThirdRoundGM", StringComparison.InvariantCultureIgnoreCase))
                {
                    headers[ThirdRoundGMInformation] = i;
                }
            }

            return headers;
        }

        protected override void FillEntity(DomainTurn entity, int index, int?[] headers, string[] data)
        {
            if (headers[Visibility].HasValue)
            {
                int value;
                if (int.TryParse(data[index + headers[Visibility].Value], out value))
                {
                    entity.Visibility = value;
                }
            }

            if (headers[State].HasValue)
            {
                int value;
                if (int.TryParse(data[index + headers[State].Value], out value))
                {
                    entity.State = value;
                }
            }

            if (headers[Domain].HasValue)
            {
                entity.Domain = this.domains
                    .Where(e => e.Name.Equals(data[index + headers[Domain].Value], StringComparison.InvariantCultureIgnoreCase))
                    .FirstOrDefault();
            }

            if (headers[TurnNumber].HasValue)
            {
                int value;
                if (int.TryParse(data[index + headers[TurnNumber].Value], out value))
                {
                    entity.Turn = value;
                }
            }

            if (headers[OwnerInformation].HasValue)
            {
                entity.TurnOwnerInformation = data[index + headers[OwnerInformation].Value];
            }

            if (headers[GMInformation].HasValue)
            {
                entity.TurnGMInformation = data[index + headers[GMInformation].Value];
            }

            if (headers[FirstRoundOwnerInformation].HasValue)
            {
                entity.FirstRoundOwnerInformation = data[index + headers[FirstRoundOwnerInformation].Value];
            }

            if (headers[SecondRoundOwnerInformation].HasValue)
            {
                entity.SecondRoundOwnerInformation = data[index + headers[SecondRoundOwnerInformation].Value];
            }

            if (headers[ThirdRoundOwnerInformation].HasValue)
            {
                entity.ThirdRoundOwnerInformation = data[index + headers[ThirdRoundOwnerInformation].Value];
            }

            if (headers[FirstRoundGMInformation].HasValue)
            {
                entity.FirstRoundGMInformation = data[index + headers[FirstRoundGMInformation].Value];
            }

            if (headers[SecondRoundGMInformation].HasValue)
            {
                entity.SecondRoundGMInformation = data[index + headers[SecondRoundGMInformation].Value];
            }

            if (headers[ThirdRoundGMInformation].HasValue)
            {
                entity.ThirdRoundGMInformation = data[index + headers[ThirdRoundGMInformation].Value];
            }
        }
    }
}
